<template>
  <div class="login_container">
    <div class="login_box">
      <h3 class="chatp">送水管理系统界面</h3>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        label-width="auto"
        :rules="rules"
      >
        <el-form-item label="账号:" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            placeholder="账号"
            autocomplete="off"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="密码:" prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            placeholder="密码"
            type="password"
            autocomplete="off"
          >
          </el-input>
        </el-form-item>

        <el-form-item style="text-align: center; margin-top: 5%">
          <el-button class="letterbtns" type="primary" @click="send()" round
            >登陆</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { defineComponent, reactive,  } from "vue";
import { login, logininfo } from "@/apis/userData.js";
import { ElMessage } from "element-plus";
import { useStore } from "vuex";
export default defineComponent({
  data() {
    return {
      name: "login",
    };
  },
  setup() {
    const loginForm = reactive({
      username: "",
      password: "",
    });

    const store= useStore();

    const rules = reactive({
      username: [
        { required: true, message: "请输入登陆名称", trigger: "blur" },
        {
          min: 1,
          max: 11,
          message: "长度在 1 到 11 个字符",
          trigger: "blur",
        },
      ],
      password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        {
          min: 3,
          max: 12,
          message: "长度在 3 到 12 个字符",
          trigger: "blur",
        },
      ],
    });

    function send() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          let name = {
            loginacct: loginForm.username,
            password: loginForm.password,
          };
          login(name)
            .then((res) => {
              if (res.data.data) {
                ElMessage({
                  message: "登陆成功",
                  type: "success",
                });
                logininfo(res.data.data.sid).then((resdata) => {
                  let data = resdata.data.staff;
                  store.commit("updatesid", data.sid);
                  store.commit("updateusername", name.username);
                  store.commit("updateName", data.sname);
                  let power = data.position;
                  let presult=true;
                  
                  if(power=="管理员"){
                    presult=false;
                  }
                  store.commit("updateposition", presult);
                  setTimeout(() => {
                    this.$router.push("/index");
                  }, 100);
                });
              } else {
                ElMessage({
                  message: "输入账号不存在或者密码错误",
                  type: "error",
                });
                return;
              }
            })
            .catch(function (err) {
              ElMessage({
                message: "错误原因" + err,
                type: "error",
              });
              return;
            });
        }
      });
    }

    return {
      loginForm,
      send,
      rules,
    };
  },
});
</script>

<style lang="less" scoped>
.chatp {
  font-family: "微软雅黑";
  color: #505458;
  text-align: center;
  font-size: 30px;
  margin-top: 30px;
  margin-bottom: 40px;
  letter-spacing: 1px;
}

.login_container {
  background-image: url("../assets/log.jpg");
  background-position: 0px 60%;
  height: 100%;
  width: 100%;
  background-size: cover;
}
.login_box {
  border-radius: 15px;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
  padding-right: 40px;
}

.rightbtns {
  position: relative;
  float: right;
}

.imgdiv {
  margin: 0px auto;
  text-align: center;
  margin-bottom: 20px;

  img {
    width: 40%;
    height: 40%;
    background-color: #eee;
  }

  a {
    float: right;
  }
}
</style>
